package net.xju3_4.dao;
import net.xju3_4.common.utils.JdbcUtilsV1;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.entity.Car;
import net.xju3_4.entity.Merchant;
import net.xju3_4.entity.MerchantEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MerchantDao {

    public static boolean updateLogo(String id, String logoUrl) {
        String sql = "UPDATE merchant SET logo_url = ? WHERE id = ?";

        try {
            int result = JdbcUtilsV1.executeUpdate(sql, logoUrl, id);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    public PageInfo<MerchantEntity> pagelist(Integer pageNo, Integer pageSize, MerchantEntity merchant) {
        String sql="select " + "id, " + "username, " + "password, " +
                "merchant_name as merchantName, " +  // 映射到实体类 merchantName
                "contact_person as contactPerson, " +  // 映射到实体类 contactPerson
                "contact_phone as contactPhone, " +  // 映射到实体类 contactPhone
                "address, " +
                "business_license as businessLicense, " +  // 映射到实体类 businessLicense
                "logo_url as logoUrl, " +"status from merchant where ";  // 映射到实体类 logoUrl
        List<Object> params=new ArrayList<>();
        if(merchant.getUsername()!=null)
        {
            sql=sql+"username like ? and ";
            params.add("%"+merchant.getUsername()+"%");
        }
        if(merchant.getMerchantName()!=null)
        {
            sql=sql+"merchant_name like ? and ";
            params.add("%"+merchant.getMerchantName()+"%");
        }
        if(merchant.getAddress()!=null){
            sql=sql+"address like ?";
            params.add("%"+merchant.getAddress()+"%");
        }
        sql=sql+"limit ?,?";
        params.add((pageNo-1)*pageSize);
        params.add(pageSize);
        List<MerchantEntity> merchants = JdbcUtilsV1.executeQuery(MerchantEntity.class,sql,params.toArray());
        String  sqlcount="select count(*) from merchant  ";
        Long count=JdbcUtilsV1.executeQueryOneValue(JdbcUtilsV1.getConnection(),sqlcount);
        PageInfo<MerchantEntity> pageInfo = new PageInfo<>(pageNo,pageSize,Math.toIntExact(count),merchants);
        return pageInfo;
    }

    public int forbid(Integer id) {
        String Sql="update merchant set status=0 where id=?";
        return JdbcUtilsV1.executeUpdate(JdbcUtilsV1.getConnection(),Sql,id);
    }

    public int activate(Integer id) {
        String Sql="update merchant set status=1 where id=?";
        return JdbcUtilsV1.executeUpdate(JdbcUtilsV1.getConnection(),Sql,id);
    }
//
    public Map<String, Integer> count() {
         String sql="select count(*) from merchant";
         Long totalMerchantCount=JdbcUtilsV1.executeQueryOneValue(JdbcUtilsV1.getConnection(),sql);
         String sql1="select count(*) from merchant where status=1";
         Long activeMerchantCount=JdbcUtilsV1.executeQueryOneValue(JdbcUtilsV1.getConnection(),sql1);
         String sql2="select count(*) from merchant where status=0";
         Long forbidMerchantCount=JdbcUtilsV1.executeQueryOneValue(JdbcUtilsV1.getConnection(),sql2);
         if(forbidMerchantCount==null)
             forbidMerchantCount=0L;
         if(activeMerchantCount==null)
             activeMerchantCount=0L;
             if(totalMerchantCount==null)
                 totalMerchantCount=0L;
         return Map.of("totalMerchant",Math.toIntExact(totalMerchantCount),"activeMerchant",Math.toIntExact(activeMerchantCount),"forbidMerchant",Math.toIntExact(forbidMerchantCount));
    }

    public void update(Merchant merchant) {
        String sql = "UPDATE merchant SET merchant_name=?,contact_person=?,contact_phone=?,address=?,business_license=?,description=?,logo_url=? WHERE id=?";
        JdbcUtilsV1.executeUpdate(sql,merchant.getMerchantName(),merchant.getContactPerson(),merchant.getContactPhone(),merchant.getAddress(),merchant.getBusinessLicense(),merchant.getDescription(),merchant.getLogoUrl(),merchant.getId());
    }

    public Merchant get(int id) {
        String sql = "SELECT id,merchant_name merchantName,contact_person contactPerson,contact_phone contactPhone,address address,business_license businessLicense,description description,logo_url logoUrl FROM merchant WHERE id=?;";
        return JdbcUtilsV1.executeQueryOne(JdbcUtilsV1.getConnection(), Merchant.class, sql, id);
    }


    public void resetPassword(Merchant merchant) {
        String sql = "UPDATE merchant SET password = ? WHERE id = ?";
        JdbcUtilsV1.executeUpdate(sql,merchant.getPassword(),merchant.getId());
    }
}
